package cn.cdeden.knowledge.utils;

import cn.cdeden.common.mybatis.validator.QueryHelpMybatisPlus;
import cn.cdeden.common.satoken.utils.LoginHelper;
import cn.cdeden.knowledge.domain.EnterpriseFilesTree;
import cn.cdeden.knowledge.domain.FilesAttention;
import cn.cdeden.knowledge.domain.PersonalFilesTree;
import cn.cdeden.knowledge.domain.TeamFilesTree;
import cn.cdeden.knowledge.domain.dto.FilesAttentionQueryParam;
import cn.cdeden.knowledge.mapper.FilesAttentionMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description // 关注工具类
 * @Date 2023/11/8 19:42
 **/
public class SetAttentionDataUtil {
    private final FilesAttentionMapper filesAttentionMapper;

    public SetAttentionDataUtil(FilesAttentionMapper fileAttentionMapper) {
        this.filesAttentionMapper = fileAttentionMapper;
    }


    /**
     * @Description // 根据条件查询信息
     * @Date 2023/11/8 19:13
     * @param fileType
     * @param id
     * @param ids
     * @return java.util.List<com.cdyd.ecm.modules.attention.domain.FilesAttention>
     **/
    private List<FilesAttention> selectList(String fileType, Long id , List<Long> ids){
        FilesAttentionQueryParam query = new FilesAttentionQueryParam();
        QueryWrapper<FilesAttention> predicate = QueryHelpMybatisPlus.getPredicate(query);
        predicate.eq("file_type",fileType);
        predicate.eq("user_id", id);
//        if (ids.size() > 1000){
            predicate.in("tree_node_id",ids);
//        }else {
//            List<List<Long>> list = Lists.partition(ids, 1000);
//            for (List<Long> longs : list) {
//                predicate.or().in("tree_node_id",longs);
//            }
//
//        }

        return  filesAttentionMapper.selectList(predicate);
    }

//
//    /**
//     * @Description // 个人关注
//     * @Date 2023/11/8 19:41
//     * @param personalFilesTrees
//     * @return java.util.List<com.cdyd.ecm.modules.personal.domain.PersonalFilesTree>
//     **/
//    public List<PersonalFilesTree> setAttentionPersonalData(List<PersonalFilesTree> personalFilesTrees) {
//        if (ObjectUtils.isNotEmpty(personalFilesTrees)) {
//            List<Long> ids = personalFilesTrees.stream().map(PersonalFilesTree::getId).collect(Collectors.toList());
////            F
////            List<FilesCollection> list = filesCollectionMapper.selectList(predicate);
//            Long id = LoginHelper.getUserId();
//            List<FilesAttention> selectList = selectList("0", id, ids);
//            if (ObjectUtils.isNotEmpty(selectList)) {
//                for (PersonalFilesTree tree : personalFilesTrees) {
//                    String treeId = String.valueOf(tree.getId());
//                    boolean b = true;
//                    if (selectList.size() == 0) {
//                        break;
//                    }
//                    for (int i = 0; i < selectList.size(); i++) {
//                        FilesAttention collection = selectList.get(i);
//                        String treeNodeId = collection.getTreeNodeId();
//                        if (StringUtils.equals(treeId,treeNodeId)) {
//                            tree.setAttention("1");
//                            b = false ;
//                            selectList.remove(i);
//                            break;
//                        }
//                    }
//
//                    if (b){
//                        tree.setAttention("0");
//                    }
//                }
//            }else {
//                for (PersonalFilesTree tree : personalFilesTrees) {
//                    tree.setAttention("0");
//                }
//            }
//        }
//        return personalFilesTrees;
//    }

    /**
     * @Description // 团队
     * @Date 2023/11/8 19:42
     * @param teamFilesTrees
     * @return java.util.List<com.cdyd.ecm.modules.team.domain.TeamFilesTree>
     **/
    public List<TeamFilesTree> setAttentionTeamData(List<TeamFilesTree> teamFilesTrees) {
        if (ObjectUtils.isNotEmpty(teamFilesTrees)) {
            List<Long> ids = teamFilesTrees.stream().map(TeamFilesTree::getId).collect(Collectors.toList());
            Long id = LoginHelper.getUserId();
            List<FilesAttention> selectList = selectList("1", id, ids);
            if (ObjectUtils.isNotEmpty(selectList)) {
                for (TeamFilesTree tree : teamFilesTrees) {
                    String treeId = String.valueOf(tree.getId());
                    if (selectList.size() == 0) {
                        break;
                    }
                    for (int i = 0; i < selectList.size(); i++) {
                        FilesAttention collection = selectList.get(i);
                        String treeNodeId = collection.getTreeNodeId();
                        if (StringUtils.equals(treeId,treeNodeId)) {
                            tree.setAttention("1");
                            selectList.remove(i);
                            break;
                        }else {
                            tree.setAttention("0");
                        }
                    }
                }
            }else {
                for (TeamFilesTree tree : teamFilesTrees) {
                    tree.setAttention("0");
                }
            }
        }

        return teamFilesTrees;
    }


    /**
     * @Description // 企业
     * @Date 2023/11/8 19:42
     * @param enterpriseFilesTrees
     * @param userId
     * @return java.util.List<com.cdyd.ecm.modules.enterprise.domain.EnterpriseFilesTree>
     **/
    public List<EnterpriseFilesTree> setAttentionEnterpriseData(List<EnterpriseFilesTree> enterpriseFilesTrees,Long userId) {

        if (ObjectUtils.isNotEmpty(enterpriseFilesTrees)) {
            List<Long> ids = enterpriseFilesTrees.stream().map(EnterpriseFilesTree::getId).collect(Collectors.toList());
            List<FilesAttention> selectList = selectList("2", userId, ids);
            if (ObjectUtils.isNotEmpty(selectList)) {
                for (EnterpriseFilesTree tree : enterpriseFilesTrees) {
                    String treeId = String.valueOf(tree.getId());
                    boolean b = true;
                    if (selectList.size() == 0) {
                        break;
                    }
                    for (int i = 0; i < selectList.size(); i++) {
                        FilesAttention collection = selectList.get(i);
                        String treeNodeId = collection.getTreeNodeId();
                        if (StringUtils.equals(treeId,treeNodeId)) {
                            tree.setAttention("1");
                            b = false ;
                            selectList.remove(i);
                            break;
                        }
                    }

                    if (b){
                        tree.setAttention("0");
                    }
                }
            }else {
                for (EnterpriseFilesTree tree : enterpriseFilesTrees) {
                    tree.setAttention("0");
                }
            }
        }




        return enterpriseFilesTrees;
    }

}
